/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using VSLangProj80;
using EnvDTE;
using System.IO;
using DaveSexton.DocProject.Wizards.Foundation;
using DaveSexton.DocProject.Wizards;
using System.Text.RegularExpressions;

namespace DaveSexton.DocProject.Sandcastle
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
	public sealed class LocateCompilersWizardStep : WizardStep
	{
		#region Public Properties
		public override string Name
		{
			get
			{
				return Resources.WizardText.LocateCompilersStepName;
			}
		}

		public override bool Visible
		{
			get
			{
				if (outputStep.ProduceHelpVersion1)
					missingHelp1x = !File.Exists(SandcastleBuildEngineProvider.Paths.HtmlHelpWorkshop);

				return outputStep.ProduceHelpVersion1 && missingHelp1x;
			}
		}

		public override bool CanPreloadUserInterface
		{
			get
			{
				return true;
			}
		}

		public string HtmlHelpWorkshopLocation
		{
			get
			{
				return Control.HtmlHelpWorkshopLocation;
			}
		}
		#endregion

		#region Private / Protected
		private LocateCompilersControl Control
		{
			get
			{
				if (control == null)
				{
					control = new LocateCompilersControl();
					control.HelpLocationChanged += delegate(object sender, EventArgs e)
					{
						OnStateChanged(new WizardStepStateChangedEventArgs(CanNavigateAway(null, null, true), true));
					};
				}

				return control;
			}
		}

		private LocateCompilersControl control;
		private readonly IDocProject project;
		private readonly ChooseCompiledHelpWizardStep outputStep;
		private bool missingHelp1x;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="LocateCompilersWizardStep" /> class.
		/// </summary>
		public LocateCompilersWizardStep(IDocProject project, ChooseCompiledHelpWizardStep outputStep)
		{
			if (project == null)
				throw new ArgumentNullException("project");

			if (outputStep == null)
				throw new ArgumentNullException("outputStep");

			this.project = project;
			this.outputStep = outputStep;
		}
		#endregion

		#region Methods
		protected override object GetUserInterface(WizardController controller, bool preloading)
		{
			return Control;
		}

		protected override bool CanNavigateAway(WizardController controller, WizardStep toStep, bool forward)
		{
			return !forward || !missingHelp1x || Control.IsHtmlHelpWorkshopLocationValid;
		}

		protected override void WizardFinished(WizardController controller)
		{
			SandcastleBuildEngineProvider provider = (SandcastleBuildEngineProvider) project.Provider;

			if (missingHelp1x)
				provider.HtmlHelpWorkshopLocation = Control.HtmlHelpWorkshopLocation;
		}

		protected override void WizardCanceled(WizardController controller)
		{
			// do nothing
		}
		#endregion
	}
}
